<template>
	
	<view style="background-color: #fff">	
			<my-search bgColor="#c00000" :radius="18" @gotoSearch="gotoSearch"></my-search>
		<view class="scroll-view-container">
			<scroll-view scroll-y class="left-scroll-view" :style="{height: wh+'px'}">
				<view class="left-srcoll-view-item" v-for="(item,index) in categoryList" :key="item.cat_id"
					@click="changeActive(index)"
					:class="[index==activeIndex?'left-srcoll-view-item active':'left-srcoll-view-item']">
					{{item.cat_name}}</view>

			</scroll-view>
			<scroll-view scroll-y class="right-scroll-view" :style="{height: wh+'px'}" :scroll-top="scrollTop">
				<view class="cate-lv2" v-for="item2 in cateLevel2" :key="item2.cat_id">
					<view class="cate-lv2-title">/ {{item2.cat_name}} /</view>
					<view class="cate-lv3">
						<view class="cate-lv3-item" v-for="item3 in item2.children" @click="gotoGoodsList(item3)">
							<image :src="item3.cat_icon" mode="" class="cate-lv3-img"></image>
							<view class="cate-lv3-title">{{item3.cat_name}}</view>
						</view>
					</view>
				</view>

			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMixin from "@/mixins/cart-badge.js"
	export default {		
		mixins: [badgeMixin],
		data() {
			return {
				// 该字段是当前设备的可用高度（屏幕高度-顶部高度-tabBar高度）
				wh: 0,
				categoryList: [],
				// 该字段用来获取点击的left-scroll-view-item的索引值，便于动态添加类名
				activeIndex: 0,
				cateLevel2: [],
				scrollTop: 0
			}
		},
		mounted() {
			let sysInfo = uni.getSystemInfoSync()
			// 这里减去的50是my-search组件的高度
			this.wh = sysInfo.windowHeight-50;
			this.getCategoryList();
		},
		methods: {
			async getCategoryList() {
				let {data: res} = await uni.$http.get("/categories")
				if (res.meta.status !== 200) return uni.$showMsg();
				this.categoryList = res.message;
				this.cateLevel2 = res.message[0].children;
			},
			changeActive(index) {
				this.activeIndex = index;
				// 当切换左侧scroll-view-item的索引值时，切换对应的 cateLevel2 的值
				this.cateLevel2 = this.categoryList[index].children;
				// 让srcoll-view组件的scroll-top值在1和0之间来回进行切换
				this.scrollTop=this.scrollTop?0:1;
			},
			gotoGoodsList(item) {
				uni.navigateTo({
					url: "/subpkg/goods_list/goods_list?cid="+item.cat_id
				})
			},
			gotoSearch() {
				uni.navigateTo({
					url: "/subpkg/search/search"
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 120px;

			.left-srcoll-view-item {
				line-height: 60px;
				background-color: #f7f7f7;
				text-align: center;
				font-size: 12px;
				position: relative;

				&.active {
					background-color: #fff;

					&::before {
						content: " ";
						width: 3px;
						height: 30px;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
					}
				}

			}
		}
		.cate-lv3 {
			display: flex;
			flex-wrap: wrap;
			
			.cate-lv3-item {
				width: 33.33%;
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-bottom: 10px;
				
				.cate-lv3-img {
					width: 60px;
					height: 60px;
					margin-bottom: 10px;
				}
				
				.cate-lv3-title {
					font-size: 12px;
				}
			}
		}
		.cate-lv2-title {
			font-size: 12px;
			font-weight: bolder;
			text-align: center;
			margin: 15px 0;
		}
	}
</style>